package com.tocean.demo.controller.admin;

import com.tocean.demo.controller.BaseController;

import com.tocean.demo.dto.ExaminationSet;
import com.tocean.demo.entity.questionbank.*;
import com.tocean.demo.service.*;

import com.tocean.demo.dto.Result;
import com.tocean.demo.entity.Attr.*;
import com.tocean.demo.vo.sys.AccountVo;
import com.tocean.demo.vo.sys.TestVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 功能描述：
 * https://www.jianshu.com/p/cf9ad8c3621d
 *
 * @Auther: 梁展鹏
 * @Date: 2018/7/24 11:38
 */
@Api("我是管理后台")
@Slf4j
@RestController
@RequestMapping("/")
public class AccountController2 extends BaseController {
    @Autowired
    private AccountService accountService;

    @Autowired
    private QuestBankService qService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ExaminationService examinationService;

    @Autowired
    private HisTestScoreService hisTestScoreService;

    @Autowired
    UserService userService;

    @Autowired
    private HisTestRecordService hisTestRecordService;

    @ApiOperation(value = "默认返回值", notes = "系统连接测试可用")
    @PostMapping("/index")
    public ResponseEntity<Result> index() {
        return new ResponseEntity(new Result().ok("connnect success", ""), HttpStatus.OK);
    }

//    @ApiOperation(value = "发送消息", notes = "系统全局异常、error日志、编程式通知等场景")
//    @PostMapping("/login")
//    public ResponseEntity<Result> login(Long openId) {
//        Boolean account = accountService.find(openId);
//        if (account){
//            return new ResponseEntity(new Result().ok("login success", "Welcome"), HttpStatus.OK);
//        }
//        return new ResponseEntity(new Result().ok("login success", "Wrong"), HttpStatus.OK);
//    }

    @ApiOperation(value = "发送消息", notes = "系统全局异常、error日志、编程式通知等场景")
    @PostMapping("/signup")
    public ResponseEntity<Result> signup(Long openId) {
        AccountVo accountVo = new AccountVo();
        accountVo.setId(openId);
        return new ResponseEntity(new Result().ok("register success"), HttpStatus.OK);
    }

    @ApiOperation(value = "发送消息", notes = "系统全局异常、error日志、编程式通知等场景")
    @PostMapping("/addAccount")
    public AccountVo addAccount(AccountVo accountVo) {
        accountService.add(accountVo);
        return accountVo;
    }


    @ApiOperation(value = "模拟题库，返回考试", notes = "根据tid")
    @PostMapping("/getUrlExamList")
    public ResponseEntity<Result> getUrlTest(String tag) {
        List<MyQuestion> info =  qService.getAllTags(tag);//访问题库的数据库
        //examinationService.AddExam(info);
        return new ResponseEntity(new Result().ok("get exam success", info), HttpStatus.OK);
    }


    @ApiOperation(value = "模拟题库，返回考试", notes = "根据tid")
    @GetMapping("/getAllLocalExamList")
    public ResponseEntity<Result> getAllLocalExamList(String type) {
        List<ExaminationSet> info =  qService.getAllExamPaper(type);//访问本地的数据库
        if(type.equals("考试")){
            for(int i=0;i<info.size();i++){
                qService.setTime(info.get(i));
            }
        }
        return new ResponseEntity(new Result().ok("get exam success", info), HttpStatus.OK);
    }

    @ApiOperation(value = "模拟题库，返回考试", notes = "根据tid")
    @GetMapping("/getAllLocalExamListByTag")
    public ResponseEntity<Result> getAllLocalExamListByTag(String tag,String type) {
        List<ExaminationSet> info =  qService.getAllExamByTags(tag,type);//访问本地的数据库
        if(type.equals("考试")){
            for(int i=0;i<info.size();i++){
                qService.setTime(info.get(i));
            }
        }
        return new ResponseEntity(new Result().ok("get exam success", info), HttpStatus.OK);
    }

    /**
     * 验证答案   并且 把此次成绩记录到数据库   hisTestScore数据库
     * @param answers
     * @return
     */
    @ApiOperation(value = "获取当前考试成绩反馈", notes = "根据ExaminationSet返回题目")
    @PostMapping("/getEResult")
    public ResponseEntity<Result> getEResult(@RequestBody ExaminationSet answers){
        int count = 0;
        for(int i=0;i<answers.getMyQuestions().size();i++){
            Boolean temp = qService.getTestAnswer(answers.getMyQuestions().get(i).getId(),answers.getMyQuestions().get(i).getChoice());
            String choice = qService.getAnswer(answers.getMyQuestions().get(i).getId());
            String[] list = choice.split("\"");
            answers.getMyQuestions().get(i).setRAnswer(list[1]);
            answers.getMyQuestions().get(i).setIsCorrect(temp);
            if(temp) {
                count++;
            }
        }
        Double score = count*100.0/answers.getMyQuestions().size();
        DecimalFormat df = new DecimalFormat("0");
        for(int i=0;i<answers.getMyQuestions().size();i++) {
            answers.getMyQuestions().get(i).setScore(df.format(score));
        }

        hisTestScoreService.AddScore(answers);
        return new ResponseEntity(new Result().ok("get Exam Result success", answers), HttpStatus.OK);
    }


    @ApiOperation(value = "添加用户信息", notes = "接收用户信息并添加")
    @PostMapping("/getUser")
    public ResponseEntity<Result> addUserInfo(User user) {
        Boolean re = userService.addUser(user);
        return new ResponseEntity(new Result().ok("Add User Information Success", re), HttpStatus.OK);
    }

    @ApiOperation(value = "查询用户信息", notes = "根据微信id返回用户信息")
    @PostMapping("/selectUser")
    public ResponseEntity<Result> selectUserInfo(Long id) {
        User user = userService.selectUser(id);
        return new ResponseEntity(new Result().ok("Find User Information Success", user), HttpStatus.OK);
    }

    @ApiOperation(value = "更改用户信息", notes = "接收用户所有信息并更改不同的数据")
    @PostMapping("/updateUser")
    public ResponseEntity<Result> updateUserInfo(User user) {

        return new ResponseEntity(new Result().ok("Find User Information Success", user), HttpStatus.OK);
    }


    /**
     * 根据请求的参数  HisTestScore  返回对应的做题记录
     * @param hisTestScore
     * @return
     */
    @PostMapping("/getHistoryRecord")
    public ResponseEntity<Result> test(@RequestBody HisTestScore hisTestScore) {
        List<HisTestRecord> hisTestRecords = hisTestRecordService.selectRecord(hisTestScore);
        return new ResponseEntity(new Result().ok("Find User Information Success", hisTestRecords), HttpStatus.OK);
    }


    /**
     * 查找该用户id的所有成绩
     * @param userId
     * @return
     */
    @GetMapping("/getHistoryScore")
    public ResponseEntity<Result> getHistoryScore(String openId) {
        List<HisTestScore> sets = hisTestScoreService.findAllScores(openId);
        return new ResponseEntity(new Result().ok("Find User Information Success", sets), HttpStatus.OK);
    }

    @PostMapping("/test")
    public ResponseEntity<Result> test(String topicId) {
        String a = qService.getAnswer(topicId);
        return new ResponseEntity(new Result().ok("Find User Information Success", a), HttpStatus.OK);
    }


}
